package com.dinggou.shopjb.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.dinggou.shopjb.R;
import com.dinggou.shopjb.interfaces.OnItemClickListener;
import com.dinggou.shopjb.view.CustomTextView;
import com.zc.mychart.model.GoodsAllBean;
import com.zc.mychart.utils.ArithUtil;

import java.util.ArrayList;
import java.util.List;

public class HomeHotItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<GoodsAllBean> dataList;
    private OnItemClickListener<GoodsAllBean> onItemClickListener;
    private Context mContext;
    private List<Integer> diffResult;

    public void setOnItemClickListener(OnItemClickListener<GoodsAllBean> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setDataList(List<GoodsAllBean> newList) {
//        if (this.dataList != null && !this.dataList.isEmpty()) {
//            LogUtil.d("dangzt", this.dataList.toString() + "后面刷新" + newList.toString());
//            if (diffResult != null) {
//                diffResult.clear();
//            }
//            diffResult = findDifferentIndices(this.dataList, newList);
//            LogUtil.d("dangzt", diffResult.toString());
//            for (int i = 0; i < 2; i++) {
//                notifyItemChanged(i);
//            }
//        }
        this.dataList = newList;
        notifyDataSetChanged();
//        notifyItemChanged(0);
    }

    public static ArrayList<Integer> findDifferentIndices(List<GoodsAllBean> list1, List<GoodsAllBean> list2) {
        ArrayList<Integer> differentIndices = new ArrayList<>();

        for (int i = 0; i < list1.size(); i++) {
            if (!list1.get(i).equals(list2.get(i))) {
                differentIndices.add(i);
            }
        }

        return differentIndices;
    }

//    public void setOpenAll(boolean open) {
//        this.isOpen = open;
//        notifyDataSetChanged();
//    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        return new HomeHotItemHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_hot_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (holder instanceof HomeHotItemHolder) {
            ((HomeHotItemHolder) holder).setData(position);
            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(dataList, v, position, holder.getItemId());
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
//        if (isOpen){
//            return dataList.size();
//        } else {
//            if (dataList.size() >= 2) {
//                return 2;
//            } else {
//                return dataList.size();
//            }
//        }

        return dataList.size();
    }

    class HomeHotItemHolder extends RecyclerView.ViewHolder {
        private final TextView tv_name;
        private final TextView tv_code;
        private final CustomTextView tv_prod_current_price;
        private final TextView tv_unit;
        private final TextView tv_prod_change_value;
        private final TextView tv_prod_change_proportion;
        private final ImageView iv_has_ticket;
        private final View view_bg;
        private AnimationSet showAnimation = null;
        private AnimationSet hideAnimation = null;
        private final long DURATION_TIME = 300;
        private double cacheLatestPrice;

        public HomeHotItemHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_code = itemView.findViewById(R.id.tv_code);
            view_bg = itemView.findViewById(R.id.view_bg);
            tv_prod_current_price = itemView.findViewById(R.id.tv_prod_current_price);
            tv_unit = itemView.findViewById(R.id.tv_unit);
            tv_prod_change_value = itemView.findViewById(R.id.tv_prod_change_value);
            tv_prod_change_proportion = itemView.findViewById(R.id.tv_prod_change_proportion);
            iv_has_ticket = itemView.findViewById(R.id.iv_has_ticket);
            setPriceAnimtion();
        }

        public void setData(int position) {
            GoodsAllBean goodsAllBean = dataList.get(position);
            clearAnim();
            if (cacheLatestPrice == goodsAllBean.latest_price) {
                return;
            }
            tv_name.setText(goodsAllBean.pro_name);
            tv_code.setText(goodsAllBean.pro_code);
            double latestPrice = goodsAllBean.latest_price;
            String handlePrice;
            if ((int) latestPrice == latestPrice) {
                handlePrice = latestPrice + "";
            } else {
                handlePrice = ArithUtil.format(ArithUtil.round(latestPrice, 2));
            }
            tv_prod_current_price.setText(handlePrice);
            tv_unit.setText("元/" + goodsAllBean.unit_cn);
            double changeValue = 0;
            double changeProportion = 0;
            if (goodsAllBean.price_end_lastday != 0) {
                changeValue = ArithUtil.sub(goodsAllBean.latest_price, goodsAllBean.price_end_lastday);
                changeProportion = ArithUtil.div(ArithUtil.mul(changeValue, 100), goodsAllBean.price_end_lastday, 2);
            }
            Boolean isClose = goodsAllBean.during_type == 0;
            if (isClose) {
                iv_has_ticket.setVisibility(View.VISIBLE);
            }else {
                iv_has_ticket.setVisibility(View.GONE);
            }
            if (changeValue >= 0) {
                int redColor = ContextCompat.getColor(mContext, R.color.buy_red);
                tv_prod_change_value.setText("+" + ArithUtil.format(changeValue));
                tv_prod_change_proportion.setText("+" + changeProportion + "%");

                tv_prod_change_value.setTextColor(redColor);
                tv_prod_change_proportion.setTextColor(redColor);
                tv_prod_current_price.setTextColor(redColor);
            } else {
                int greenColor = ContextCompat.getColor(mContext, R.color.buy_green);
                tv_prod_change_value.setText(ArithUtil.format(changeValue) + "");
                tv_prod_change_proportion.setText(changeProportion + "%");

                tv_prod_change_value.setTextColor(greenColor);
                tv_prod_change_proportion.setTextColor(greenColor);
                tv_prod_current_price.setTextColor(greenColor);
            }
            if (cacheLatestPrice > 0){
                if (goodsAllBean.latest_price - cacheLatestPrice >= 0){
                    startUpAnim();
                }else {
                    startDownAnim();
                }
            }


            cacheLatestPrice = goodsAllBean.latest_price;
        }

        private void setPriceAnimtion() {
            hideAnimation = new AnimationSet(true);
            hideAnimation.setDuration(DURATION_TIME);
            AlphaAnimation alphadown = new AlphaAnimation(1, 0);
            alphadown.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    view_bg.setBackgroundColor(Color.TRANSPARENT);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            hideAnimation.addAnimation(alphadown);

            showAnimation = new AnimationSet(true);
            showAnimation.setDuration(DURATION_TIME);
            AlphaAnimation alpha = new AlphaAnimation(0, 1);
            alpha.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    view_bg.startAnimation(hideAnimation);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            showAnimation.addAnimation(alpha);
        }

        private void startUpAnim() {
            view_bg.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_33F24957));
            view_bg.startAnimation(showAnimation);
        }

        private void startDownAnim() {
            view_bg.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_331DBF60));
            view_bg.startAnimation(showAnimation);
        }

        private void clearAnim(){
            view_bg.setBackgroundColor(Color.TRANSPARENT);
            view_bg.clearAnimation();
        }
    }
}
